{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from lxml import etree\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'http://angelimg.spbeen.com/ang/10200/1'\n",
    "headers = {\n",
    "    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36'\n",
    "}\n",
    "all_links = [url]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# response = requests.get(url, headers=headers, timeout=(3, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# html = etree.HTML(response.text)\n",
    "# base_url = 'http://angelimg.spbeen.com'\n",
    "# next_links = html.xpath('.//div[@id=\"page\"]/a/@href')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['/ang/10200/2',\n",
       " '/ang/10200/3',\n",
       " '/ang/10200/4',\n",
       " '/ang/10200/5',\n",
       " '/ang/10200/6',\n",
       " '/ang/10200/7',\n",
       " '/ang/10200/2']"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next_links"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_next_links(url):\n",
    "    ## 给定一个链接，返回下一个链接\n",
    "    resp = requests.get(url, headers=headers)\n",
    "    ## 将拿到的响应解析一下，然后就得到了html对象\n",
    "    html = etree.HTML(resp.text)\n",
    "    next_links = html.xpath('.//div[@id=\"page\"]/a/@href')\n",
    "    for i in next_links:\n",
    "        base_url = 'http://angelimg.spbeen.com'\n",
    "        next_url = base_url + i\n",
    "        if next_url not in all_links:\n",
    "            all_links.append(next_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前请求的链接是： http://angelimg.spbeen.com/ang/10200/1\n",
      "当前请求的链接是： http://angelimg.spbeen.com/ang/10200/2\n",
      "当前请求的链接是： http://angelimg.spbeen.com/ang/10200/3\n",
      "当前请求的链接是： http://angelimg.spbeen.com/ang/10200/4\n",
      "当前请求的链接是： http://angelimg.spbeen.com/ang/10200/5\n",
      "当前请求的链接是： http://angelimg.spbeen.com/ang/10200/6\n",
      "当前请求的链接是： http://angelimg.spbeen.com/ang/10200/7\n",
      "当前请求的链接是： http://angelimg.spbeen.com/ang/10200/8\n",
      "当前请求的链接是： http://angelimg.spbeen.com/ang/10200/9\n",
      "当前请求的链接是： http://angelimg.spbeen.com/ang/10200/10\n",
      "当前列表的链接内容为： ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "for link in all_links:\n",
    "    print('当前请求的链接是：', link)\n",
    "    get_next_links(link)\n",
    "    time.sleep(1)\n",
    "print('当前列表的链接内容为：', [i.split('/')[-1] for i in all_links])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 去重的逻辑代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 8]\n"
     ]
    }
   ],
   "source": [
    "links = [1, 2, 3, 4, 5, 6, 7]\n",
    "for i in range(3, 9):\n",
    "    links.append(i)\n",
    "print(links)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8]\n"
     ]
    }
   ],
   "source": [
    "links = [1, 2, 3, 4, 5, 6, 7]\n",
    "for i in range(3, 9):\n",
    "    if i not in links:\n",
    "        links.append(i)\n",
    "print(links)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
